var express = require("express");
var router = express.Router();
const { exec } = require("child_process");

/* GET home page. */
router.get("/getok", function (req, res, next) {
  console.log("getok");
  res.send({
    title: new Date(),
  });
});

//打包的测试接口
router.get("/build", function (req, res) {
  const frontendProjectPath = "E:\\BaiduNetdiskDownload\\Vue.js\\vue_build";
  const component = req.query.component || ""; // 从查询参数中获取要打包的组件名称
  let buildCommand = "npm run build";

  if (component) {
    buildCommand = `cross-env BUILD_COMPONENT=${component} npm run build`;
  }

  exec(buildCommand, { cwd: frontendProjectPath }, (err, stdout, stderr) => {
    if (err) {
      console.error(`exec error: ${err}`);
      return res.status(500).send(`Error during build: ${err.message}`);
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
    res.send("打包成功" + component);
  });
});

router.get("/getdata", (req, res) => {
  res.send({
    title: req.query.id,
  });
});

module.exports = router;
